from django.shortcuts import render
from django.http import HttpResponse
from .models import *

# 增删改查
# def addcode(request):
#     # save
#     storeuser = StoreUser()
#     storeuser.username = "zs"
#     storeuser.password = "111111"
#     storeuser.nick_name = "张三"
#     storeuser.age = 19
#     storeuser.save()
#
#     # create  有返回值
#     # data = StoreUser.objects.create(username="ls",password="111",nick_name="李四",age=20)
#     return HttpResponse("add code")
#
# def getcode(request):
#     # all 返回所有符合条件的数据
#     # data = StoreUser.objects.all()
#     # """QuerySet"""
#     # print(data)
#     # for one in data:
#     #     zz = one.get_gender_display()
#     #     print(zz)
#
#     # get 返回有且只有一条的数据
#     # data = StoreUser.objects.get(id = 1)
#     # print(data.nick_name)
#
#     # filter
#     # data = StoreUser.objects.filter(username="zs")
#     # """QuerySet"""
#     # print(data)
#
#     # first
#     # data = StoreUser.objects.filter(username="zs").first()
#     # """对象"""
#     # print(data)
#
#     # last
#     # data = StoreUser.objects.filter(username="zs").last()
#     # """对象"""
#     # print(data)
#
#     # values
#     # data = StoreUser.objects.values()
#     # """特殊的QuerySet，列表包含字典的形式"""
#     # print(data)
#
#     # order_by
#     # data = StoreUser.objects.order_by("-id")
#     # """QuerySet"""
#     # print(data)
#
#     # exists
#     # data = StoreUser.objects.filter(username="zs").exists()
#     # """布尔值"""
#     # print(data)
#
#     # data = StoreUser.objects.filter(username="zs").order_by("-id")
#     # print(data)
#
#     # data = StoreUser.objects.filter(username="zs").order_by("-id").values()
#     # print(data)
#
#     # data = StoreUser.objects.filter(nick_name="南俊伟").first()
#     # print(data)
#
#     # 聚合
#     from django.db.models import Sum,Avg,Min,Max,Count
#     # Sum
#     # data = StoreUser.objects.aggregate(ages = Sum("age"))
#     # print(data)
#
#     # Avg
#     # data = StoreUser.objects.aggregate(v_age = Avg("age"))
#     # print(data)
#
#     # Min
#     # data = StoreUser.objects.aggregate(Min("age"))
#     # print(data)
#
#     # Max
#     # data = StoreUser.objects.aggregate(Max("nick_name"))
#     # print(data)
#
#     # Count
#     # data = StoreUser.objects.aggregate(Count("nick_name"))
#     # print(data)
#
#     # group_by
#     # data = StoreUser.objects.values("nick_name").annotate(Count("username"))
#     # print(data)
#
#     # data = StoreUser.objects.values("username").annotate(Count("username"))
#     # print(data)
#
#     # data = StoreUser.objects.values("id").annotate(Max("age"))
#     # print(data)
#
#     from django.db.models import Q,F
#     # data = StoreUser.objects.filter(Q(age__gt=12) & Q(gender=1))
#     # print(data)
#
#     # data = StoreUser.objects.filter(age__gt=F("id")).all()
#     # print(data)
#
#     # data = StoreUser.objects.filter(id = 5).values()
#     # print(data)
#
#     return HttpResponse("get code")
#
# def updatecode(request):
#     # StoreUser.objects.filter(id = 1).update(username="njw1")
#     storeuser = StoreUser.objects.get(id = 3)
#     storeuser.username = "ndg"
#     storeuser.save()
#     return HttpResponse("update code")
#
# def deletecode(request):
#     # StoreUser.objects.all().delete()
#     StoreUser.objects.filter(id = 6).delete()
#     StoreUser.objects.get(id = 7).delete()
#     return HttpResponse("delete code")

def add_storeuser(request):
    # storeuser = StoreUser.objects.create(
    #     username="meili",
    #     password="123456",
    #     nick_name="美丽",
    #     age=23
    # )
    # store = Store.objects.create(
    #     store_name="爱美商店",
    #     store_desc="这是一个爱美丽的商店",
    #     store_address="这是你猜不到的敌方地方",
    #     store_user=storeuser
    # )

    storeuser = StoreUser.objects.create(
        username="meili1",
        password="0123456",
        nick_name="美丽1",
        age=23
    )
    store = Store.objects.create(
        store_name="爱美商店1",
        store_desc="这是一个爱美丽的商店1",
        store_address="这是你猜不到的敌方地方1",
        store_user_id=storeuser.id
    )
    return HttpResponse("one to one")

def get_storeuser(request):
    # 从店铺获取店铺老板的信息
    # store = Store.objects.get(id = 1)
    # storeuser_id = store.store_user_id
    # storeuser = StoreUser.objects.get(id = storeuser_id)
    # print(storeuser)

    # 从老板获取店铺的信息
    storeuser = StoreUser.objects.filter(username="美丽 ").first()

    # data = storeuser.store
    # print(data)
    return HttpResponse("获取到了")


def ontomany(request):
    # 一对多
    # 增加
    # goods_type = GoodType.objects.create(
    #     t_name="水果",
    #     t_desc = "水果"
    # )
    # goods_type = GoodType.objects.create(
    #     t_name="蔬菜",
    #     t_desc="多吃蔬菜，身体健康"
    # )
    # goods = Goods()
    # goods.goods_name = "西瓜"
    # goods.goods_num = 19
    # goods.goods_price = 11.2
    # # 第一种 对应的外键id增加
    # goods.goods_type_id = goods_type.id
    # goods.save()

    # 第二种 对象增加
    # goods_type = GoodType.objects.filter(t_name = "水果").first()
    # goods = Goods()
    # goods.goods_name = "香蕉"
    # goods.goods_price = 12.3
    # goods.goods_num = 10
    # goods.goods_type = goods_type
    # goods.save()

    # goods_type = GoodType.objects.filter(t_name="蔬菜").first()
    # goods = Goods()
    # goods.goods_name = "黄瓜"
    # goods.goods_num = 10
    # goods.goods_price = 10.4
    # goods.goods_type = goods_type
    # goods.save()

    # 查询
    # 正向从Goods到GoodsType
    # goods = Goods.objects.filter(goods_name="香蕉").first()
    # goods_type = goods.goods_type
    # print(goods_type.t_name)

    # 黄瓜对应的类型
    # goods = Goods.objects.filter(goods_name="黄瓜").first()
    # goods_type = goods.goods_type
    # print(goods_type.t_name)

    # 反向从GoodsType到Goods
    # goods_type = GoodType.objects.filter(t_name="蔬菜").first()
    # goods = goods_type.goods_set.all().values("goods_name")
    # goods = goods_type.goods_set.first().goods_name
    # print(goods)

    # 水果类型对应的商品名称
    # goods_type = GoodType.objects.filter(t_name="水果").first()
    # goods = goods_type.goods_set.all().values("goods_name")
    # print(goods)
    return HttpResponse("one to many")

def manytomany(request):
    # 增加
    # store = Store.objects.filter(store_name="爱美商店").first()
    # store1 = Store.objects.filter(store_name="爱美商店1").first()
    # goods_type = GoodType.objects.filter(t_name="蔬菜").first()
    # goods_type.goodstype_store.set([store,store1])
    # goods_type.save()

    # 查询
    # 店铺爱美商店对应的类型 反向
    # store = Store.objects.filter(store_name="爱美商店").first()
    # goods_type = store.goodtype_set.all()
    # print(goods_type)

    # 水果类型在那个店铺
    # goods_type = GoodType.objects.filter(t_name="水果").first()
    # store = goods_type.goodstype_store.all()
    # print(store)
    return HttpResponse("many to many")

def bookstore(request):
    # 1.查找所有书名里包含金老板的书
    book = Book.objects.filter(title__contains="金老板").all()

    # 2.查找价格大于10元的书名和价格
    book = Book.objects.filter(price__gt=10).values("price","title")

    # 3.查找名字以沙河开头的出版社
    pubname = Publisher.objects.filter(name__startswith="沙河").all()

    # 4.查找书名是“跟金老板学开车”的书的出版社出版的其他书籍的名字和价格
    book = Book.objects.filter(title="跟金老板学开车").first().Publisher.book_set.all().values("title","price")

    return HttpResponse("查询到了")



